package com.link.base.base.questionsurvey.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.user.model.User;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.util.UserUtil;
import com.link.base.base.questionsurvey.model.Question;
import com.link.base.base.questionsurvey.model.QuestionAnswer;
import com.link.base.base.questionsurvey.service.QuestionService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.StringUtils;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 问卷调查
 */
@Controller
@RequestMapping("/link/questionsurvey")
public class QuestionController extends BasicController<Question> {

    @Resource
    private QuestionService questionService;
    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Override
    public BasicService<Question> getBasicService() throws Exception {
        return questionService;
    }

    /** 查询问卷列表*/
    @RequestMapping(value = "/queryQuestionSurveyPage")
    @ResponseBody
    public Map<String, Object> queryList(@JsonParam QueryParams qps, @JsonParam Question entity, HttpSession session, HttpServletRequest request,
                                         HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            Question t = (Question) BasicModel.transformClass(entity.getClass(), qps);
            beforQueryAllOrExample(t, request);
            beforQueryAll(t, request);
            List<Question> list = questionService.queryQuestionSurveyPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Question>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 发布出去的问卷列表
     */
    @RequestMapping(value = "/queryQuestionByUserPage")
    @ResponseBody
    public Map<String, Object> queryQuestionByUserPage(@JsonParam QueryParams qps, @JsonParam Question entity, HttpSession session,
                                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            Question t = (Question) BasicModel.transformClass(entity, qps);
            CoreUser user = UserUtil.getUser();
            List<Question> list = questionService.queryQuestionByUserPage(t, user.getId());
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Question>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Question>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 新建问卷
     */
    @RequestMapping(value = "/questionInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> questionInsert(@RequestBody Question record, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Long quesId = record.getId();
            quesId = keyGenerateDao.keyGenerate();
            record.setId(quesId);
            questionService.questionTitleInsert(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/answerInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> answerInsert(@RequestBody QuestionAnswer record, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CoreUser user = UserUtil.getUser();
            Long userId = user.getId();
            Long questionId = record.getQuestionId();
            User usrerParam = new User();
            usrerParam.setPageFlag(false);
            usrerParam.setId(userId);
            //问卷ID
            usrerParam.setAttr1(String.valueOf(questionId));
            List<User> userList = questionService.queryUserFillInPage(usrerParam);
            if (userList.size() < 1) {
                throw new BasicControllerException("没有填写的权限，请联系管理员");
            }
            if (userList.size() > 1) {
                throw new BasicControllerException("不能重复填写");
            }
            String flag = "Y";
            if (userList.size() == 1 && StringUtils.equals(userList.get(0).getAttr2(),flag )) {
                throw new BasicControllerException("不能重复填写");
            }
            record.setEmpId(userId);
            questionService.answerInsert(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/queryQuestionById")
    @ResponseBody
    public Map<String, Object> queryQuestionById(@JsonParam Question entity, HttpSession session, HttpServletRequest request,
                                                 HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Question question = questionService.queOneQuestionDetailInfo(entity.getId());
            result.put("success", true);
            result.put("rows", question);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Question>());
            result.put("result", e.getMessage());
        }
        return result;
    }

}
